package com.example.demo.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.List;

/**
 * 里面可以继承ddlScript 数据库脚本执行包
 * <p>
 * 配置mybatis-plus
 */
@Configuration
public class MpsConfig {


    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig conf = new GlobalConfig();
//        conf.setDbConfig(new GlobalConfig.DbConfig().setColumnFormat("%s"));// 3.4.X版本之后才有这个方法
//        // 默认sql注入
//        DefaultSqlInjector defaultSqlInjector = new DefaultSqlInjector() {
//
//            /**
//             * 配置注入自定义方法
//             * InsertBatchSomeColumn 自定义批量插入
//             * AlwaysUpdateSomeColumnById 总是根据id修改某些值
//             * @param mapperClass
//             * @param tableInfo
//             * @return
//             */
//            @Override
//            public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
//                List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
//
//                // 此处自定义方法
//                /**
//                 * 配置批量插入方法 里面加入条件
//                 * 1 不要逻辑删除字段
//                 * 2 排除乐观锁字段
//                 * 3 排除策略设置中 不是修改字段
//                 */
//                methodList.add(new InsertBatchSomeColumn(t -> !t.isLogicDelete()
//                        && !t.isVersion() && t.getFieldFill() != FieldFill.UPDATE));
//
//                /**
//                 * 1 排除insert策略字段
//                 * 2 排除字段名为column4的字段
//                 */
//                methodList.add(new AlwaysUpdateSomeColumnById(x -> x.getFieldFill() != FieldFill.INSERT
//                        && !x.getProperty().equals("column4")));
//                return methodList;
//            }
//        };
//        conf.setSqlInjector(defaultSqlInjector); // 设置sql注入
        return conf;
    }


    /**
     * 配置sqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);// 配置数据源
        factoryBean.setTypeEnumsPackage("");// 配置枚举包扫描路径
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/*Mapper.xml"));

        // mybatis configuration配置
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);// 驼峰命名规则
        factoryBean.setConfiguration(mybatisConfiguration);

        // mybatis 配置连接器
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 分页
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());// 乐观锁
        factoryBean.setPlugins(mybatisPlusInterceptor);

        globalConfig.setMetaObjectHandler(new MysqlMetaObjectHandler());// 配置自动填充插件
        factoryBean.setGlobalConfig(globalConfig);
        return factoryBean.getObject();
    }
}
